package com.jason.platform.supervision.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.*;
import com.hzlj.supervision.changsha.R;
import com.jason.platform.supervision.BootApp;
import com.jason.platform.supervision.adapter.NotifyAdapter;
import com.jason.platform.supervision.base.Constant;
import com.jason.platform.supervision.beans.InfoPublishBean;
import com.jason.platform.supervision.beans.NotifyResp;
import com.jason.platform.supervision.task.QueryNotifyListTask;
import com.jason.platform.supervision.task.Task;
import com.jason.platform.supervision.task.TaskListener;
import com.jason.platform.supervision.utils.SharedPreferencesUtils;
import com.jason.platform.supervision.widght.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: NotifyActivity
 * @Description: 事件通知
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-25 09:34:47
 */
public class NotifyActivity extends BaseActivity implements TaskListener {

    private static final String TAG = NotifyActivity.class.getSimpleName();

    private MyHandler<NotifyActivity> mHandler;
    private View mFootView;

    private TitleBar mTitleBar;

    private ListView mListView;
    private NotifyAdapter mAdapter;

    private List<InfoPublishBean> mInfoPublishBeans = new ArrayList<InfoPublishBean>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_notify);
        super.onCreate(savedInstanceState);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.bindActivity(this);
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        mHandler = new MyHandler<NotifyActivity>(this);
        mListView = (ListView) findViewById(R.id.notify_list);
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(this, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.GONE);
        mListView.addFooterView(mFootView);
        mAdapter = new NotifyAdapter(this);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                InfoPublishBean bean = mInfoPublishBeans.get(position);
                Intent intent = new Intent(NotifyActivity.this,NotifyDetailActivity.class);
                intent.putExtra(NotifyDetailActivity.KEY_NOTIFY_INFO,bean);
                startActivity(intent);
            }
        });
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    if (view.getLastVisiblePosition() == (view.getCount() - 1)) {//滚动到底部
                        if (!mIsLoading && mHasNextPage) {
                            loadMore();
                        }else {
                            if (mFootView != null) {
                                mFootView.setVisibility(View.GONE);
                            }
                        }
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        mIsLoading = true;
        mIsStart = true;
        mCurrentIndex = 0;
        QueryNotifyListTask task = new QueryNotifyListTask(this);
        task.type = Constant.InfoPublish.QUERY;
        task.mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        task.mParams.put("organId", SharedPreferencesUtils.getOrganId(this).toString());
        task.mParams.put("imie", BootApp.getInstance().getImei());
        task.execute();
    }

    private void loadMore(){
        if (mFootView == null) {
            mFootView = LinearLayout.inflate(this, R.layout.widget_listview_footer, null);
        }
        mFootView.setVisibility(View.VISIBLE);
        mListView.setSelection((mListView.getAdapter().getCount() - 1));
        mIsLoading = true;
        mIsStart = false;
        QueryNotifyListTask task = new QueryNotifyListTask(this);
        task.type = Constant.InfoPublish.QUERY;
        task.mParams.put("currentIndex", String.valueOf(++mCurrentIndex));
        task.mParams.put("organId", SharedPreferencesUtils.getOrganId(this).toString());
        task.mParams.put("imie", BootApp.getInstance().getImei());
        task.execute();
    }

    @Override
    public void onTaskResult(Task task) {
        Message msg = new Message();
        msg.obj = task;
        mHandler.sendMessage(msg);
    }

    @Override
    void handleMessage(BaseActivity activity, Message msg) {
        Task task = (Task) msg.obj;
        if (task.type == Constant.InfoPublish.QUERY){
            if ( mLoadingDialog != null){
                mLoadingDialog.dismiss();
            }
            if (task.isSuccess) {
                NotifyResp resp = (NotifyResp) task.resData;
                if (mIsStart){
                    mInfoPublishBeans.clear();
                }
                mInfoPublishBeans.addAll(resp.getValues());
                if (mInfoPublishBeans != null && !mInfoPublishBeans.isEmpty()){
                    if (mAdapter != null){
                        mAdapter.addAll(mInfoPublishBeans);
                        mAdapter.notifyDataSetChanged();
                    }
                }
                mHasNextPage = !resp.isLastPage();
                mIsLoading = false;
                if (mHasNextPage) {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.VISIBLE);
                    }
                } else {
                    if (mFootView != null) {
                        mFootView.setVisibility(View.GONE);
                    }
                }
            }else {
                if (mAdapter != null){
                    mAdapter.addAll(new ArrayList<InfoPublishBean>());
                    mAdapter.notifyDataSetChanged();
                }
                if (mFootView != null) {
                    mFootView.setVisibility(View.GONE);
                }
                Toast.makeText(this, task.rspDesc, Toast.LENGTH_SHORT).show();
            }
        }
    }
}
